package com.ymatou.iapi.productspu.testcase;

import com.ymatou.iapi.productspu.parameter.GetSpuPropertyListBean;
import com.ymatou.iapi.productspu.parameter.PropertyOut;
import com.ymatou.iapi.productspu.service.GetSpuPropertyListCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.database.sqlwapper.SpuPropertyWapper;
import com.ymttest.utils.Logger2Debug;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*获取SPU属性列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetSpuPropertyList{
	private static GetSpuPropertyListBean getspupropertylistBean;
	private static GetSpuPropertyListCall getspupropertylistCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取SPU属性列表");
	}

	@Before
	public void caseUp() {
		getspupropertylistBean=new GetSpuPropertyListBean();
		getspupropertylistCall=new GetSpuPropertyListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getspupropertylistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(Integer spuId, List<PropertyOut> pout) throws InterruptedException {
		SpuPropertyWapper SpuPropertyWapper=new SpuPropertyWapper();
		List<Map> spupinfo = SpuPropertyWapper.selectSpuPropertyBySpuId(spuId);
		Map<String,Map> mspu=new HashMap<>();
		spupinfo.forEach(sp->{
			mspu.put(sp.get("PropertyId").toString()+sp.get("InputValues").toString(),sp);
		});

		Logger.debug("mspu:"+mspu);
		int i=0;
		pout.forEach(po->{
			Logger.debug(FormatUtil.beanToGSONString(po));
			String key ="";
			if(po.getInputValues()==null||po.getInputValues().isEmpty()){
				 key = po.getPropertyId().toString();
			}else{
				key = po.getPropertyId()+(po.getInputValues()==null?"":po.getInputValues().get(0));
			}
			Map spu = mspu.get(key);
			Logger.verifyEquals(spu.get("Name"), po.getPropertyName(), "验证PropertyName");
			Logger.verifyEquals(spu.get("MultiSelect"), po.getIsMultiple(), "验证MultiSelect");
			Logger.verifyEquals(spu.get("Styles"), po.getPropertyValueType(), "验证Styles");
		});
		
		
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSpuPropertyList_001() {
		Logger.start(true,"获取SPU属性列表成功");
		try {
			Integer spuid=262936;
			getspupropertylistBean.setSPUId(spuid);
			getspupropertylistCall.setData(getspupropertylistBean);
			getspupropertylistCall.callService();			
			List<PropertyOut> pout = getspupropertylistCall.getPropertyList();
			
			List<Map> spucinfo =new SpuPropertyWapper().selectSpuCategoryBySpuId(spuid);
			Logger.verifyEquals(spucinfo.get(0).get("ProductName"),getspupropertylistCall.getJsonObject().get("Result").getAsJsonObject().get("SpuName").getAsString(),"验证SpuName");
			Logger.verifyEquals("200", getspupropertylistCall.getString("Status"), "验证Status");
			verifyDB(spuid, pout);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetSpuPropertyList_002() {
		Logger.start(true,"不输入ProductId失败");
		try {
			getspupropertylistCall.setData(getspupropertylistBean);
			getspupropertylistCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetSpuPropertyList_003() {
		Logger.start(true,"核对全部数据,获取SPU属性列表成功");
		try {
			//存在问题 属性值id重复导致对比获取数据错误
			new SpuPropertyWapper().selectAllSpuProduct().forEach(spup->{
				getspupropertylistBean.setSPUId(spup.getSpuId());
				getspupropertylistCall.setData(getspupropertylistBean);
				try {
					Logger2Debug.debug(spup.getSpuId());
					getspupropertylistCall.callService();
					Logger2Debug.verifyEquals("200", getspupropertylistCall.getString("Status"), "验证Status");
//					verifyDBDebug(spup.getSpuId(),getspupropertylistCall.getSPU());
				} catch (Exception e) {
					e.printStackTrace();
				}	
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}